package com.hmk.blogboot.web;


import com.hmk.blogboot.po.Tag;
import com.hmk.blogboot.service.BlogService;
import com.hmk.blogboot.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TagShowController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TagService tagService;

    //在tags页面进行展示tag列表和 blog内容
    @GetMapping("/tags/{id}")
    public String tags(@PageableDefault(size =8,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,
                       @PathVariable Long id, Model model){
        //根据指定展示tag条数  获取list<tag>
        List<Tag> tags = tagService.listTagTop(10000);
        //判断是否tagId==-1
        if(id == -1){
            //说明 首页跳转到tags页面 tagId ==  tags列表第一个tag的id
            id = tags.get(0).getId();
        }
        model.addAttribute("tags",tags);
        model.addAttribute("page",blogService.listBlog(id,pageable));
        model.addAttribute("activeTagId",id);
        return "tags";


    }



}
